package servlet.events;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.security.NoSuchAlgorithmException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bundle.ErrorsBundleKey.UserErrorBundleKey;

import model.User;
import dao.DAOFactory;
import util.FormValidation;
import util.SimpleMD5;

public class RegisterHandler extends EventHandler {	
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			if (validateForm(request)) 
			{
				User user = getUser(request);	
				if (DAOFactory.getUserDAO().insert(user))
					request.setAttribute("status", "ok");
				else {
					request.setAttribute("status", "error");
					setErrors(UserErrorBundleKey.REGISTER_FAIL, request);
				}
			}
			forward(urlBundle.getString("REGISTER_URL"), request, response);
	}	
	
	private boolean validateForm(HttpServletRequest request) {
		int i = 0;
//		ResourceBundle bundle = ResourceBundle.getBundle("Errors");
		
		if(request.getParameter(USER_LASTNAME) == null || request.getParameter(USER_LASTNAME).trim().equals("")){
//			request.setAttribute("LASTNAME_NOT_NULL", bundle.getString("LASTNAME_NOT_NULL"));
			setErrors(UserErrorBundleKey.LASTNAME_NOT_NULL, request);
			i++;
		}
		
		if(request.getParameter(USER_FIRSTNAME) == null || request.getParameter(USER_FIRSTNAME).trim().equals("")){
//			request.setAttribute("FIRSTNAME_NOT_NULL", bundle.getString("FIRSTNAME_NOT_NULL"));
			setErrors(UserErrorBundleKey.FIRSTNAME_NOT_NULL, request);
			i++;
		}
		
		if(request.getParameter(USER_LOGIN) == null || request.getParameter(USER_LOGIN).trim().equals("")){
//			request.setAttribute("LOGIN_NOT_NULL", bundle.getString("LOGIN_NOT_NULL"));
			setErrors(UserErrorBundleKey.LOGIN_NOT_NULL, request);
			i++;
		}
		
		if(request.getParameter(USER_PASSWORD) == null || request.getParameter(USER_PASSWORD).trim().equals("")){
//			request.setAttribute("PASSWORD_NOT_NULL", bundle.getString("PASSWORD_NOT_NULL"));
			setErrors(UserErrorBundleKey.PASSWORD_NOT_NULL, request);
			i++;
		}
		
		if (request.getParameter(USER_EMAIL) == null || request.getParameter(USER_EMAIL).trim().equals("")){
//			request.setAttribute("EMAIL_NOT_NULL", bundle.getString("EMAIL_NOT_NULL"));
			setErrors(UserErrorBundleKey.EMAIL_NOT_NULL, request);
			i++;
		}
		//else if (!user.getEmail().matches("^[a-zA-Z0-9]+([-+.'][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*\.[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*$") {
		else if(!FormValidation.validateEmail(request.getParameter(USER_EMAIL))){
//			request.setAttribute("EMAIL_INVALID", bundle.getString("EMAIL_INVALID"));
			setErrors(UserErrorBundleKey.EMAIL_INVALID, request);
			i++;
		}
		
		if(request.getParameter(USER_ADDRESS) == null || request.getParameter(USER_ADDRESS).trim().equals("")){
//			request.setAttribute("ADDRESS_NOT_NULL", bundle.getString("ADDRESS_NOT_NULL"));
			setErrors(UserErrorBundleKey.ADDRESS_NOT_NULL, request);
			i++;
		}
		
		if (i == 0)
			return true;
		
		return false;
	}
	
	
	private User getUser(HttpServletRequest request) {
		User user = new User();
		user.setFirstname(request.getParameter(USER_FIRSTNAME));
		user.setLastname(request.getParameter(USER_LASTNAME));
		user.setLogin(request.getParameter(USER_LOGIN));
		try {
			user.setPassword(SimpleMD5.encrypt(request.getParameter(USER_PASSWORD)));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		user.setEmail(request.getParameter(USER_EMAIL));
		user.setAddress(request.getParameter(USER_ADDRESS));
		user.setGroup(0);
		user.setGender(Integer.parseInt(request.getParameter(USER_GENDER)));
		return user;
	}
	
	private static final String USER_FIRSTNAME = "firstname";
	private static final String USER_LASTNAME = "lastname";
	private static final String USER_LOGIN = "login";
	private static final String USER_PASSWORD = "password";
	private static final String USER_EMAIL = "email";
	private static final String USER_ADDRESS = "address";
	private static final String USER_GENDER = "gender";
	
}